package com.ourway.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.liuweiheng.base.constant.redis.GlobalStrRedisConstant;
import com.liuweiheng.base.entity.R;
import com.liuweiheng.base.utils.GlobalStrRedisUtil;
import com.ourway.entity.vo.UserAccessTokenInfoVo;
import com.ourway.service.IFeiShuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 账号控制器
 *
 * @author lwh
 * @date 2021/12/02
 */
@RestController
@RequestMapping("/feishu/account")
@Slf4j
public class FeiShuAccountController {

    private final IFeiShuService IFeiShuService;

    public FeiShuAccountController(IFeiShuService IFeiShuService) {
        this.IFeiShuService = IFeiShuService;
    }

    /**
     * 检查token
     * @param token 登录token
     */
    @GetMapping("/checkToken")
    public R checkToken(@RequestParam String token) {
        boolean isLogin = GlobalStrRedisUtil.hasKey(GlobalStrRedisConstant.USER_LOGIN_TOKEN + "_" + token);
        return isLogin ? R.ok() : R.error("token不正确");
    }

    /**
     * 登录回调接口
     */
    @GetMapping("/loginCallBack")
    public R loginCallBack(@RequestParam String code, HttpServletResponse response) throws IOException {
        if (StrUtil.isBlank(code)) {
            return R.error("Bad Request:code");
        }

        // 获取 tenant_access_token
        String tenantAccessToken = IFeiShuService.getTenantAccessToken();

        // 获取 user_access_token
        UserAccessTokenInfoVo userAccessTokenInfo = IFeiShuService.getUserAccessToken(tenantAccessToken, code);

        if (null == userAccessTokenInfo) {
            return R.error("user not exist");
        }

        log.info(userAccessTokenInfo.toString());

        // 登录
        String token = IFeiShuService.userLogin(userAccessTokenInfo);

        response.sendRedirect("http://localhost:9527/#/login?adminToken=" + token);
        return R.ok();
    }

    /**
     * 获取登录用户信息
     *
     * @param token 登录凭证
     * @return 用户信息
     */
    @GetMapping("/info/v")
    public R info(@RequestParam String token) {
        if (checkToken(token).hasError()) {
            return R.error("token不存在");
        }

        String jsonStr = GlobalStrRedisUtil.getValue(GlobalStrRedisConstant.USER_LOGIN_TOKEN + "_" + token);

        UserAccessTokenInfoVo data = JSONUtil.toBean(jsonStr, UserAccessTokenInfoVo.class);

        return R.ok().putData(data);
    }

    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        String token = request.getHeader("X-Token");
        if (null == token || token.isEmpty()) {
            return R.error("Bad Request: Request Header X-Token Is Empty");
        }
        String key = GlobalStrRedisConstant.USER_LOGIN_TOKEN + "_" + token;
        // 清除token
        if (GlobalStrRedisUtil.hasKey(key)) {
            GlobalStrRedisUtil.delKey(key);
        }
        return R.ok();
    }

}
